package pers.cocoadel.cloud.movie.feign;

import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import pers.cocoadel.cloud.movie.domain.User;

import java.math.BigDecimal;

@Service
@FeignClient(name = "user-service", configuration = UserFeignConfig.class,
        fallbackFactory = UserClient.UserFeignClientFallbackFactory.class)
public interface UserClient
{
    @GetMapping("/users/{id}")
    User findById(@PathVariable("id") Long id);


    class UserFeignClientFallback implements UserClient
    {
        @Override
        public User findById(Long id)
        {
            return new User(id, "Weiss ", "Weiss ",
                    0, new BigDecimal(1));
        }
    }

    @Component
    @Slf4j
    class UserFeignClientFallbackFactory implements FallbackFactory<UserClient>
    {
        @Override
        public UserClient create(Throwable throwable)
        {
            log.error("进入返回逻辑", throwable);
            return new UserFeignClientFallback();
        }
    }
}
